package com.test.kotlin.ui.adapter

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.test.kotlin.R
import com.test.kotlin.bean.LinkData


class HomeLinkAdapter : RecyclerView.Adapter<HomeLinkAdapter.HomeLinkViewHolder> {
    var viewType:Int=0
    var viewCount:Int=0

    var isLookMore=true
    var linkData:MutableList<LinkData> = mutableListOf()

    constructor(type:Int,count:Int){
        this.viewType=type
        this.viewCount=count
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeLinkViewHolder {
        var view:View?=null
        lateinit var holder: HomeLinkViewHolder
        when(viewType){
            0x0001 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_link_item,parent,false)
            0x0002 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_link_more_item,parent,false)
        }
        holder= view?.let{
            HomeLinkViewHolder(it,viewType)
        }!!
        return holder
    }

    override fun getItemCount(): Int {
        return viewCount
    }

    override fun onBindViewHolder(holder: HomeLinkViewHolder, position: Int) {
        when(viewType){
            0x0001 -> {
                LogUtils.e("显示链接数据...")
                linkData?.let {
                    if(position<it.size){
                        with(it[position]) {
                            Glide.with(holder.ivLinkImg)
                                .load(file_root)
                                .placeholder(R.drawable.place_holder)
                                .into(holder.ivLinkImg)
                        }
                    }
                }
            }
            0x0002 -> {
                holder.tvLinkMore.text=if(isLookMore) "查看更多" else "收起"
                holder.tvLinkMore.setOnClickListener {
                    if(isLookMore){
                        linkData?.let {
                            viewCount=it.size+1
                            isLookMore=false
                            notifyItemRangeInserted(6,viewCount-6)
                        }
                    }else{
                        var count=viewCount-6
                        viewCount=6
                        isLookMore=true
                        notifyItemRangeRemoved(6,count)
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        if(viewType==0x0001&&viewCount>=6&&position==viewCount-1){
            if(viewCount==6&&isLookMore){
                return viewType+1
            }else if(viewCount>6){
                return viewType+1
            }
        }
        return viewType
    }

    fun updateLinkData(datas:MutableList<LinkData>?){
        datas?.let {
            LogUtils.e("更新数据...")
            viewCount=if(it.size>6){
                isLookMore=true
                6
            } else it.size
            if(linkData.isNotEmpty()){
                linkData.clear()
            }
            linkData.addAll(datas)
            LogUtils.e("viewCount=$viewCount")
            notifyDataSetChanged()
        }
    }


    class HomeLinkViewHolder(view:View,type:Int) : RecyclerView.ViewHolder(view){
        lateinit var ivLinkImg: ImageView
        lateinit var tvLinkMore: TextView
        init {
            when(type){
                0x0001,0x0002 -> {
                    itemView.layoutParams.width=(ScreenUtils.getScreenWidth()- SizeUtils.dp2px(32f))/3
                    itemView.layoutParams.height=itemView.layoutParams.width/2
                    itemView.requestLayout()
                    when(type){
                        0x0001 -> ivLinkImg=itemView as ImageView
                        0x0002 -> tvLinkMore=itemView as TextView
                    }
                }
            }
        }
    }


}